package com.qlc_demo_api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qlc_demo_api.mapper.QlcOprSettingMapper;
import com.qlc_demo_api.service.QlcSettingService;
import com.qlc_demo_api.util.HttpClientUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class QlcSettingServiceImpl implements QlcSettingService {
    @Resource
    QlcOprSettingMapper qlcOprSettingMapper;


    /***
     * 获取secret
     * @param appId
     * @param settingCode
     * @return 返回数据库中settingCode对应secret
     */
    @Override
    public String getSecret(String appId, String settingCode) {
        return qlcOprSettingMapper.getSecret(appId, settingCode);
    }

    /***
     * 获取微信用户信息
     * @param appId
     * @param settingCode
     * @param code
     * @return 返回用户信息
     */
    @Override
    public Map<String, Object> getUserInfo(String appId, String settingCode, String code) {
        String secrect = getSecret(appId, settingCode);
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+secrect+"&code="+code+"&grant_type=authorization_code";
        Map<String, Object> responseDataMap = getWechatAuthInfo(url);
        if (responseDataMap.get("errcode") != null) {
            responseDataMap.put("errMsg", responseDataMap.get("errmsg"));
            return responseDataMap;
        }
        String accessToken = (String) responseDataMap.get("access_token");
        String openId = (String) responseDataMap.get("openid");
        String refreshToken = (String) responseDataMap.get("refresh_token");
        // 获取用户信息
        return getWechatUserInfo(accessToken, openId, appId, refreshToken);
    }

    /**
     * 刷新token
     * @param appId
     * @param refreshToken
     * @return
     */
    @Override
    public Map<String, Object> refreshTokenFn(String appId, String refreshToken) {
        String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="+ appId +"&grant_type=refresh_token&refresh_token="+refreshToken;
        Map<String, Object> responseDataMap = getWechatAuthInfo(url);
        if (responseDataMap.get("errcode") != null) {
            responseDataMap.put("errMsg", responseDataMap.get("errmsg"));
            return responseDataMap;
        }
        String accessToken = (String) responseDataMap.get("access_token");
        String openId = (String) responseDataMap.get("openid");
        String newRefreshToken = (String) responseDataMap.get("refresh_token");
        return getWechatUserInfo(accessToken, openId, appId, newRefreshToken);
    }

    @Override
    public Map<String, Object> getUserInfoWXMP(String appId, String code) {
        String secret = getSecret(appId, "mini1528401");
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
        Map<String, Object> responseDataMap = getWechatAuthInfo(url);
        if (responseDataMap.get("errcode") != null) {
            responseDataMap.put("errMsg", responseDataMap.get("errmsg"));
            return responseDataMap;
        }
        String openId = (String) responseDataMap.get("openid");
        String accessToken = appId+"_"+openId;
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("openId", openId);
        userInfo.put("accessToken", accessToken);
        return userInfo;
    }

    /**
     * 获取微信用户信息
     * @param accessToken
     * @param openId
     * @param appId
     * @param refreshToken
     * @return
     */
    private Map<String, Object> getWechatUserInfo(String accessToken, String openId, String appId, String refreshToken) {
        // 获取用户信息
        String getUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+ openId+"&lang=zh_CN";
        Map<String, Object> wechatUserInfoMap = getWechatAuthInfo(getUserInfoUrl);
        if (wechatUserInfoMap.get("errcode") != null) {
            wechatUserInfoMap.put("errMsg", wechatUserInfoMap.get("errmsg"));
            return wechatUserInfoMap;
        }
        // 组装前端需要的用户数据
        Map<String, Object> userInfoMap = new HashMap<String, Object>();
        userInfoMap.put("accessToken", accessToken);
        userInfoMap.put("openId", openId);
        userInfoMap.put("nickname", wechatUserInfoMap.get("nickname"));
        userInfoMap.put("appId", appId);
        userInfoMap.put("refreshToken", refreshToken);
        switch ((int) wechatUserInfoMap.get("sex")) {
            case 1:
                userInfoMap.put("sex", "男");
                break;
            case 2:
                userInfoMap.put("sex", "女");
                break;
            default:
                userInfoMap.put("sex", "未知");
        }
        userInfoMap.put("headImgUrl", wechatUserInfoMap.get("headimgurl"));
        return userInfoMap;
    }

    /**
     * 请求微信API
     * @param url
     * @return
     */
    private Map<String, Object> getWechatAuthInfo(String url) {
        String responseDataStr = HttpClientUtil.sendHttpGet(url);
        return JSONObject.parseObject(responseDataStr);
    }
}
